package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcGifts;
import com.cloud.bc.mapper.BcGiftsMapper;
import com.cloud.bc.service.BcGiftsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * 礼物
 *
 * @author douhaichao code generator
 * @date 2021-01-09 15:41:00
 */
@Service
@AllArgsConstructor
public class BcGiftsServiceImpl extends ServiceImpl<BcGiftsMapper, BcGifts> implements BcGiftsService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcGifts selectOne(BcGifts req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcGifts>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcGifts> selectAll(BcGifts req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        queryWrapper.orderByDesc("sort");
        queryWrapper.orderByAsc("id");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcGifts> selectPage(Page page, BcGifts req) {
        IPage<BcGifts> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcGifts>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcGifts> selectPageByLike(Page page, BcGifts req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (!StrUtil.isEmpty(req.getTitle())) {
            queryWrapper.like("title", req.getTitle());
        }

        if (!StrUtil.isEmpty(req.getCover())) {
            queryWrapper.like("cover", req.getCover());
        }

        if (!StrUtil.isEmpty(req.getExplains())) {
            queryWrapper.like("explains", req.getExplains());
        }

        if (null != req.getPrice()) {
            queryWrapper.eq("price", req.getPrice());
        }

        if (null != req.getSort()) {
            queryWrapper.eq("sort", req.getSort());
        }

        if (null != req.getDisplay()) {
            queryWrapper.eq("display", req.getDisplay());
        }

        if (null != req.getIsDel()) {
            queryWrapper.eq("is_del", req.getIsDel());
        }
        IPage<BcGifts> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcGifts req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
